package fun.boomcat.luckyhe.mirai.plugin;

import fun.boomcat.luckyhe.mirai.plugin.command.OpReplyCommand;
import fun.boomcat.luckyhe.mirai.plugin.command.ReplyCommand;
import fun.boomcat.luckyhe.mirai.plugin.config.ConfigOperation;
import fun.boomcat.luckyhe.mirai.plugin.event.AutoReplyMessageListenerHost;
import net.mamoe.mirai.console.command.CommandManager;
import net.mamoe.mirai.console.extension.PluginComponentStorage;
import net.mamoe.mirai.console.permission.Permission;
import net.mamoe.mirai.console.permission.PermissionId;
import net.mamoe.mirai.console.permission.PermissionRegistryConflictException;
import net.mamoe.mirai.console.permission.PermissionService;
import net.mamoe.mirai.console.plugin.jvm.JavaPlugin;
import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescriptionBuilder;
import net.mamoe.mirai.event.GlobalEventChannel;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

public final class AutoReply2 extends JavaPlugin {

//    单例（官方模板）
    public static final AutoReply2 INSTANCE = new AutoReply2();

    private Permission userPermission;

    private Permission opPermission;

    private AutoReply2() {
//        官方模板
        super(new JvmPluginDescriptionBuilder(
                "luckyhe.autoreply2",
                "2.0"
        ).build());
    }

    @Override
    public void onLoad(@org.jetbrains.annotations.NotNull PluginComponentStorage $this$onLoad) {
//        获取config文件
        try {
            ConfigOperation.initConfigPath(getConfigFolder(), getResource("config.properties", StandardCharsets.UTF_8));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onEnable() {
//        启用时进行的加载与注册行为
        INSTANCE.getLogger().info("开始加载插件");
        loadPermissions();
        INSTANCE.getLogger().info("注册权限完成");
        loadEvents();
        INSTANCE.getLogger().info("注册事件完成");
        loadCommands();
        INSTANCE.getLogger().info("注册指令完成");
    }

    @Override
    public void onDisable() {

    }

    public void loadPermissions() {

//        我也不知道为什么，反正这样能用
        PermissionId userPermissionId = new PermissionId("autoreply2", "command.reply");
        PermissionId opPermissionId = new PermissionId("autoreply2", "command.opreply");

//        注册权限
        try {
            userPermission = PermissionService.getInstance().register(
                    userPermissionId,
                    "自动回复相关指令",
                    Permission.getRootPermission()
            );
            opPermission = PermissionService.getInstance().register(
                    opPermissionId,
                    "自动回复相关管理指令",
                    Permission.getRootPermission()
            );
        } catch (PermissionRegistryConflictException e) {
            e.printStackTrace();
        }
    }

    public void loadEvents() {
//        注册消息事件
        GlobalEventChannel.INSTANCE.registerListenerHost(new AutoReplyMessageListenerHost(INSTANCE));
//        GlobalEventChannel.INSTANCE.subscribeAlways(MessageEvent.class, MessageHandler::handle);

    }

    public void loadCommands() {

//        别名定义
        String[] replyCommandSecondNames = {"回复", };

//        指令注册
        CommandManager.INSTANCE.registerCommand(new ReplyCommand(
                this,
                "reply",
                replyCommandSecondNames,
                CommandManager.INSTANCE.getCommandPrefix() + "reply <操作>",
                "自动回复相关指令",
                userPermission,
                false,
                INSTANCE
        ), false);

        String[] replyOpCommandSecondNames = {"管理回复"};
        CommandManager.INSTANCE.registerCommand(new OpReplyCommand(
                this,
                "opreply",
                replyOpCommandSecondNames,
                CommandManager.INSTANCE.getCommandPrefix() + "opreply <操作>",
                "管理自动回复相关指令",
                opPermission,
                false,
                INSTANCE
        ), false);
    }
}
